﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Windows.Interop;

using System.IO;
using System.Xml;
using System.Xml.XPath;

using System.ComponentModel;
using System.Windows.Controls.Primitives;

using System.Data;
using Bainx.Data;
using Bainx.Extend;
using Bainx;

using System.Threading;
using Bainx.WPF.Extend;
using Bainx.WPF.Extend.Control;


namespace WM360Client
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : WPFBaseWindow
    {
        /// <summary>
        /// 悬浮窗体,运行在单独线程中的。
        /// </summary>
        public  SuspendWindow suspendWindow;
        /// <summary>
        /// 操作用户数据登录注销等。
        /// </summary>
        private User user;

        /// <summary>
        /// 标签浏览器
        /// </summary>
        private TabWebBrowser twb;

        

        public MainWindow()
        {
            try
            {
                Thread.CurrentThread.Name = "网脉主窗体线程";

                InitializeComponent();

                AllowDrop = true;
                
                this.Closing += new CancelEventHandler(MainWindow_Closing);
                this.Loaded += new RoutedEventHandler(Window_Loaded);
                
                this.DragEnter+=new DragEventHandler(Window_DragEnter);
                this.Drop += new DragEventHandler(Window_Drop);
                this.DragMoveWindowBegin += new MouseEventHandler(MainWindow_DragMoveWindowBegin);
                this.DragMoveWindowEnd += new MouseEventHandler(MainWindow_DragMoveWindowEnd);

                this.LoginRegister.LeftButton.Content = "登录";
                this.LoginRegister.RightButton.Content = "注册";
                this.LoginRegister.LeftButton.Click += new RoutedEventHandler(LeftLoginButton_Click);
                this.LoginRegister.RightButton.Click += new RoutedEventHandler(RightRegisterButton_Click);

                this.Activated += new EventHandler(MainWindow_Activated);

                user = new User();
            }
            catch (Exception ex)
            {
                //Information.Show(ex,true);
                ex.MessageToLogFile();
                App.Current.Shutdown(-1);
            }
            
        }

        void MainWindow_Activated(object sender, EventArgs e)
        {
            this.LoginRegister.LButton.Focus();
        }

        void MainWindow_DragMoveWindowEnd(object sender, MouseEventArgs e)
        {
            twb.Reposition();
            twb.RepositionEnd();
        }

        void MainWindow_DragMoveWindowBegin(object sender, MouseEventArgs e)
        {
            //throw new NotImplementedException();
            twb.RepositionBegin();
            
        }

        void RightRegisterButton_Click(object sender, RoutedEventArgs e)
        {
            SkinWindow skinWindow = new SkinWindow();
           skinWindow.Owner = this;
            skinWindow.ShowDialog();
        }

        void LeftLoginButton_Click(object sender, RoutedEventArgs e)
        {
            LoginWindow loginWindow = new LoginWindow();
            loginWindow.Owner = this;
            loginWindow.ShowDialog();
        }


        
        

       


       



        protected override  void WinMin_Click(object sender, RoutedEventArgs e)
        {           
            //this.ShowInTaskbar = false;
            base.WinMin_Click(sender, e);
            //Visibility = System.Windows.Visibility.Hidden;
           // Hide();
           
        }

        protected override void WinMax_Click(object sender, RoutedEventArgs e)
        {
            base.WinMax_Click(sender, e);
                        
        }

        protected override void WinClose_Click(object sender, RoutedEventArgs e)
        {
            //this.Close();
            //suspendWindow.InvokeIfNeeded(() => suspendWindow.Close(), System.Windows.Threading.DispatcherPriority.Send);
            //this.Close();
            WinMin_Click(sender, e);
        }

        void MainWindow_Closing(object sender, CancelEventArgs e)
        {
            if(suspendWindow!=null)
                suspendWindow.InvokeIfNeeded(() => suspendWindow.Close(), System.Windows.Threading.DispatcherPriority.Send);          
        }

      

        private void ButLoginSubmit_Click(object sender, RoutedEventArgs e)
        {
            //string username = this.TBoxUserName.Text.Trim();
            //string pwd = this.PBoxPassword.Password.Trim();
            //try
            //{
            //    StackPanelLogin.Visibility = System.Windows.Visibility.Hidden;
                
            //    if (user.Login(username, pwd))
            //    {
            //        GridUserInfo.Visibility = System.Windows.Visibility.Visible;
            //        UserNameLabel.Content = user.Nickname;
            //    }
            //    else
            //        throw user.Exception;
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message+"\n"+ex.GetType().ToString());
            //    StackPanelLogin.Visibility = System.Windows.Visibility.Visible;
            //}
            //finally
            //{ 
                
            //}
            
        }
       

        private void GridFrame_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if(twb!=null)
                twb.Reposition(App.Current.MainWindow);
        }



        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            ImageBrush ib = new ImageBrush(new BitmapImage(new Uri(String.Format("pack://siteoforigin:,,,/image/{0}", user.Background))));
            ib.Opacity = 0.75;
            this.Background = ib;

     
           // RunSuspendWindow();
            SuspendWindowThreadRunFunction();
            twb = new Bainx.WPF.Extend.Control.TabWebBrowser(App.Current.MainWindow);
            GridFrame.Children.Add(twb);


          // GridFrame.Children.Add(tabbb());

            twb.Add(new Uri("http://www.wmw.cn"));
            twb.Add(new Uri("http://zxzx.wmw.cn"));
        }

      


        #region //运行悬浮窗与托盘图标
        /// <summary>
        /// 运行悬浮窗与托盘图标。
        /// </summary>
        //private void RunSuspendWindow()
        //{
        //    Thread SuspendWindowThread = new Thread(new ThreadStart(SuspendWindowThreadRunFunction));
        //    SuspendWindowThread.Name = "悬浮窗体线程";
        //    SuspendWindowThread.SetApartmentState(ApartmentState.STA);
        //    SuspendWindowThread.IsBackground = true;
        //    SuspendWindowThread.Start();
            
        //}
        private void SuspendWindowThreadRunFunction()
        {
            suspendWindow = new SuspendWindow(this);
            suspendWindow.Show();
            //suspendWindow.InvokeIfNeeded(() => suspendWindow.a(), System.Windows.Threading.DispatcherPriority.Send);
           // MessageBox.Show(suspendWindow.Dispatcher.Thread.Name);
            //suspendWindow.InvokeIfNeeded(() => suspendWindow.Show(), System.Windows.Threading.DispatcherPriority.Send);
            //System.Windows.Threading.Dispatcher.Run();
        }
       
        #endregion



        #region  //拖动图片到窗体时设置成窗体背景
        private void Window_DragEnter(object sender, DragEventArgs e)
        {

            // If the data is a file or a bitmap, display the copy cursor.
            if (e.Data.GetDataPresent(DataFormats.Bitmap) ||
               e.Data.GetDataPresent(DataFormats.FileDrop))
            {                
                e.Effects = DragDropEffects.Copy;
            }
            else
            {
                e.Effects = DragDropEffects.None;
            }
        }

        private void Window_Drop(object sender, DragEventArgs e)
        {
            // Handle FileDrop data.
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                // Assign the file names to a string array, in 
                // case the user has selected multiple files.
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
                try
                {
                    // Assign the first image to the picture variable.
                    //this.picture = Image.FromFile(files[0]);
                    DateTime dt = System.DateTime.Now;
                    string[] kzm = files[0].Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
                    string filename = dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString()
                                        + dt.Hour.ToString() + dt.Minute.ToString() + dt.Second.ToString() + dt.Millisecond.ToString() + "."+kzm[kzm.Length-1];

                    System.IO.File.Copy(files[0], System.AppDomain.CurrentDomain.BaseDirectory + "image\\" + filename);

                    ImageBrush ib = new ImageBrush(new BitmapImage(new Uri("pack://siteoforigin:,,,/image/" + filename)));
                    ib.Opacity = 0.75;
                    this.Background = ib;
                    
                    // Set the picture location equal to the drop point.
                   // this.pictureLocation = this.PointToClient(new Point(e.X, e.Y));
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    ex.MessageToLogFile();
                    return;
                }
            }

            // Handle Bitmap data.
            //if (e.Data.GetDataPresent(DataFormats.Bitmap))
            //{
            //    try
            //    {
            //        // Create an Image and assign it to the picture variable.
            //        //this.picture = (Image)e.Data.GetData(DataFormats.Bitmap);
            //        // Set the picture location equal to the drop point.
            //       // this.pictureLocation = this.PointToClient(new Point(e.X, e.Y));
            //        //MessageBox.Show(files[0]);
            //    }
            //    catch (Exception ex)
            //    {
            //        MessageBox.Show(ex.Message);
            //        return;
            //    }
            //}
            // Force the form to be redrawn with the image.
            //this.Invalidate();
        }
        #endregion //拖动图片到窗体时设置成窗体背景


    }
   

   
}